#####################################################################
# Table 1
#####################################################################

library(xtable)
library(writexl)

#####################################################################
# Formulas
#####################################################################

# Expected length
E_length <- function(K,c0,alpha){
  g <- (c0<1)*K+(1<=c0)*(c0<= K)*(K/c0)+(c0>K)
  leng <- 2*qt(1-alpha/2,df=K-1)/(sqrt(K)*sqrt(K-1))*sqrt(1+min(c0,K))*sqrt(g)*sqrt(2)*(gamma(K/2)/gamma((K-1)/2))
  return(leng)
}

# Limit of expected length  
lim_E_length <- function(c0,alpha){
  return(2*qnorm(1-alpha/2)*sqrt(min((1/c0),1))*sqrt(1+c0))
}
    
#####################################################################
# Results 
#####################################################################

# Compute numbers
alpha <- 0.1
K_vec <- seq(2,10,1) 
T0    <- 30

c0 <- 30/16

rel_efficiency <-rep(NA,length(K_vec))

lim_leng <- lim_E_length(c0,alpha)
for (i in 1:length(K_vec)){
  rel_efficiency[i] <-  lim_leng/E_length(K_vec[i],c0,alpha)
}


# Table 1

print("LaTeX output corresponding to Table 1")
results_table1 <- rbind(100*rel_efficiency)
print(xtable(cbind(results_table1),digits=c(rep(2,1+length(K_vec)))),include.rownames = F)

data_results_table1 <- as.data.frame(sprintf("%.2f",results_table1))
colnames(data_results_table1) <- NULL
write_xlsx(data_results_table1,"tables/table1.xlsx")

# Numbers in text

round(100*(rel_efficiency[3]-rel_efficiency[2]))
round(100*(rel_efficiency[4]-rel_efficiency[2]))
round(100*(rel_efficiency[5]-rel_efficiency[2]))


